package dao

import (
	"fmt"
	"log"

	"order-srv/basic/inits"
	__ "order-srv/basic/proto"
	"order-srv/handler/models"
	"strconv"
)

type DistributionRest struct {
	Pattern                     string `json:"pattern"`                     //模式  1 指定分销 2 人人分销 3 满额分销
	Me                          string `json:"me"`                          //自购不反
	PullPeopleReturnPrice       string `json:"pullPeopleReturnPrice"`       //拉取人返回多少钱
	Member                      string `json:"member"`                      //是否给用户发分销佣金  0不发 1不限制
	OneRebate                   string `json:"oneRebate"`                   //一级返佣百分比
	TwoRebate                   string `json:"twoRebate"`                   //二级返佣百分比
	TPriceTime                  string `json:"TPriceTime"`                  //冻结时间
	EndPrice                    string `json:"endPrice"`                    //提现最低金额
	WithdrawType                string `json:"withdrawType"`                //提现方式
	WithdrawTypeProceduresPrice string `json:"withdrawTypeProceduresPrice"` //提现手续费
}

var RuleData = DistributionRest{
	Pattern:                     "1",  //模式  1 指定分销 2 人人分销 3 满额分销
	Me:                          "2",  //自购不反
	PullPeopleReturnPrice:       "3",  //拉取人返回多少钱
	Member:                      "0",  //是否给用户发分销佣金  0不发 -1不限制
	OneRebate:                   "20", //一级返佣百分比
	TwoRebate:                   "30", //二级返佣百分比
	TPriceTime:                  "3",  //冻结时间 按天数
	EndPrice:                    "6",  //体现最低金额
	WithdrawType:                "1",  //提现方式  1支付宝  2 微信 3数字人名币 4银联
	WithdrawTypeProceduresPrice: "3",  //提现手续费
}

func GetDistributionUpOneUserId(in *__.UpdateOrderReq) (uint64, uint64) {
	var order models.Order
	var err error
	var runWater models.DistributionRunWater
	var Distribution models.DistributionSearch
	order.Id = in.Id
	if err = order.DistributionGetOrderId(); err != nil {
		return 0, 0
	}
	Distribution.UserId = uint64(order.UserId)
	if err = Distribution.GetDistributionUpOneUser(); err != nil {
		return 0, 0
	}
	runWater.OrderSn = order.OrderNo
	fmt.Println("订单编号为", order.OrderNo)
	runWater.OrderPrice = uint32(order.OrderPrice)
	runWater.UserId = uint64(order.UserId)
	runWater.ToUserId = Distribution.FId
	WithdrawTypeProceduresPrice, err := strconv.Atoi(RuleData.WithdrawTypeProceduresPrice)
	runWater.WithdrawTypeProceduresPrice = uint32(float64(WithdrawTypeProceduresPrice))
	runWater.Price = uint32(Distribution.Price) - uint32(WithdrawTypeProceduresPrice)
	runWater.WithdrawType = uint64(in.PayCType)
	runWater.Source = "一级分销"
	runWater.Level = 1
	if err = runWater.AddDistributionRunWater(); err != nil {
		fmt.Println("一级分销流水添加失败")
		return 0, 0
	}
	fmt.Println("返回的用户Id和跟给他人Id", runWater.UserId, runWater.ToUserId)
	return runWater.UserId, runWater.ToUserId
}

func GetDistributionUpTwoUserId(in *__.UpdateOrderReq) {
	var err error
	var Distribution models.DistributionSearch
	var runWater models.DistributionRunWater
	var order models.Order
	order.Id = in.Id
	if err = order.DistributionGetOrderId(); err != nil {
		log.Println("订单Id不存在")
	}
	userId, OneUserId := GetDistributionUpOneUserId(in)
	inits.DB.Where("user_id = ?", OneUserId).Find(&Distribution)
	runWater.OrderSn = order.OrderNo
	runWater.OrderPrice = uint32(order.OrderPrice)
	runWater.UserId = userId
	runWater.ToUserId = Distribution.FId
	WithdrawTypeProceduresPrice, err := strconv.Atoi(RuleData.WithdrawTypeProceduresPrice)
	runWater.Price = uint32(Distribution.Price) - uint32(WithdrawTypeProceduresPrice)
	runWater.WithdrawTypeProceduresPrice = uint32(WithdrawTypeProceduresPrice)
	runWater.WithdrawType = uint64(in.PayCType)
	runWater.Source = "二级分销"
	runWater.Level = 2
	if err = runWater.AddDistributionRunWater(); err != nil {
		log.Println("二级加流水表失败")
	}
}
